<?php
class TermController extends Controller
{
	public $layout="//layouts/nolayout";
	
	public function actionShowTermPage()
	{
		$this->render('showTermPage');
	}
	
	public function actionAddNewTerm()
	{
		if(isset($_POST['name'])&&isset($_POST['start_date'])&&isset($_POST['total_weeks']))
		{
			$res=Term::model()->findByAttributes(array('name'=>$_POST['name']));
			if (!empty($res))
			{
				$json=array(  
							'success'=>false,
							'res'=>'添加失败！已有同名学期存在！'
						);   
				echo CJSON::encode($json);
				return;
			}
			
			$newTerm=new Term;
			$newTerm->name=$_POST['name'];
			$newTerm->total_weeks=$_POST['total_weeks'];
			//循环进行添加周次
			//使用内置函数取星期一的日期作为起始日期
			$start_date=$_POST['start_date'];
			$time=strtotime('+1 day '.$start_date);
			$start_date=date('Y-m-d',$time);
			$time=strtotime('last Monday '.$start_date);
			$start_date=date('Y-m-d',$time);
			
			//取第一周
			$newWeek=new Week;
			$newWeek->start_date=$start_date;
			$newWeek->week_describe='第1周';
			$newWeek->save();
			
			//学期保存
			$newTerm->start_week=$newWeek->week_id;
			$newTerm->status=0;
			if ($newTerm->save()>0)
			{
				$json=array(  
							'success'=>true,
							'res'=>$newTerm->term_id
						);   
				echo CJSON::encode($json);
			}else{
				$json=array(  
							'success'=>false,
							'res'=>'添加失败！'
						);   
				echo CJSON::encode($json);
				return;
			}
			//保存学期星期关系
			$newTW=new Term_Week;
			$newTW->term_id=$newTerm->term_id;
			$newTW->week_id=$newWeek->week_id;
			$newTW->save();
			//循环进行添加剩余周次
			for ($i=1;$i<$newTerm->total_weeks;$i++)
			{
				$newWeek=new Week;
				$newWeek->week_describe='第'.($i+1).'周';
				$time=strtotime('+1 week '.$start_date);
				$start_date=date('Y-m-d',$time);
				$newWeek->start_date=$start_date;
				$newWeek->save();
				//保存学期星期关系
				$newTW=new Term_Week;
				$newTW->term_id=$newTerm->term_id;
				$newTW->week_id=$newWeek->week_id;
				$newTW->save();
			}
		}else
		{
			$json=array(  
						'success'=>false,
						'res'=>'数据发送失败！'
					);   
			echo CJSON::encode($json);
		}
	}
	
	public function actionEditTerm()
	{
		if(isset($_POST['term_id'])&&isset($_POST['name']))
		{
			$res=Term::model()->findByPk($_POST['term_id']);
			if (empty($res))
			{
				$json=array(  
							'success'=>false,
							'res'=>'修改失败！无该学期存在！'
						);   
				echo CJSON::encode($json);
				return;
			}
			$res->name=$_POST['name'];
			if ($res->save()>0)
			{
				$json=array(  
							'success'=>true,
							'res'=>'修改成功！'
						);   
				echo CJSON::encode($json);
			}else{
				$json=array(  
							'success'=>false,
							'res'=>'修改失败！'
						);   
				echo CJSON::encode($json);
				return;
			}
		}else{
			$json=array(  
						'success'=>false,
						'res'=>'数据发送失败！'
					);   
			echo CJSON::encode($json);
		}
	}
	
	public function actionDeleteTerm()
	{
		if(isset($_POST['term_id']))
		{
			$res=Term::model()->findByPk($_POST['term_id']);
			if (empty($res))
			{
				$json=array(  
							'success'=>false,
							'res'=>'删除失败！无该学期存在！'
						);   
				echo CJSON::encode($json);
				return;
			}
			
			if ($res->delete()>0)
			{
				$json=array(  
							'success'=>true,
							'res'=>'删除成功！'
						);   
				echo CJSON::encode($json);
			}else{
				$json=array(  
							'success'=>false,
							'res'=>'删除失败！'
						);   
				echo CJSON::encode($json);
				return;
			}
		}else{
			$json=array(  
						'success'=>false,
						'res'=>'数据发送失败！'
					);   
			echo CJSON::encode($json);
		}
	}
	
	public function actionAddNewHoliday()
	{
		if(isset($_POST['name'])&&isset($_POST['start_date'])&&isset($_POST['end_date']))
		{
			$res=Holiday::model()->findByAttributes(array('name'=>$_POST['name']));
			if (!empty($res))
			{
				$json=array(  
							'success'=>false,
							'res'=>'添加失败！已有同名假期存在！'
						);   
				echo CJSON::encode($json);
				return;
			}
			$newHoliday = new Holiday;
			$newHoliday->name=$_POST['name'];
			$newHoliday->start_date=$_POST['start_date'];
			$newHoliday->end_date=$_POST['end_date'];
			if ($newHoliday->save()>0)
			{
				$json=array(  
						'success'=>true,
						'res'=>'添加成功！'
					);   
				echo CJSON::encode($json);
			}else{
				$json=array(  
						'success'=>false,
						'res'=>'添加失败！'
					);   
				echo CJSON::encode($json);
			}
		}else
		{
			$json=array(  
						'success'=>false,
						'res'=>'数据发送失败！'
					);   
			echo CJSON::encode($json);
		}
	}
	
	public function actionEditHoliday()
	{
		if(isset($_POST['holiday_id'])&&isset($_POST['start_date'])&&isset($_POST['end_date']))
		{
			$holiday=Holiday::model()->findByPk($_POST['holiday_id']);
			if (empty($holiday))
			{
				$json=array(  
							'success'=>false,
							'res'=>'修改失败！！找不到该假期！'
						);   
				echo CJSON::encode($json);
				return;
			}
			if (!empty($_POST['name']))
				$holiday->name=$_POST['name'];
			$holiday->start_date=$_POST['start_date'];
			$holiday->end_date=$_POST['end_date'];
			if ($holiday->save()>0)
			{
				$json=array(  
						'success'=>true,
						'res'=>'修改成功！'
					);   
				echo CJSON::encode($json);
			}else{
				$json=array(  
						'success'=>false,
						'res'=>'修改失败！'
					);   
				echo CJSON::encode($json);
			}
		}else
		{
			$json=array(  
						'success'=>false,
						'res'=>'数据发送失败！'
					);   
			echo CJSON::encode($json);
		}
	}
	
	
	public function actionGetTermList()
	{
		$termList = Term::model()->findAll();
		$results = array();
		for($i = 0;$i < count($termList);$i++)
		{
			$elem = $termList[$i]->attributes;
			$elem['start_date'] = $termList[$i]->startWeek->start_date;
			array_push($results,$elem);
		}
		echo CJSON::encode($results);
	}
	
	public function actionGetHolidayList()
	{
		$holidayList = Holiday::model()->findAll();
		echo CJSON::encode($holidayList);
	}
	
	public function actionGetWeekList($tid)
	{
		$weekList=Term_Week::model()->findAllByAttributes(array('term_id'=>$tid));
		$json=array();
		for ($i=0;$i<count($weekList);$i++)
		{
			$time=strtotime('+6 day '.$weekList[$i]->week->start_date);
			$endDate=date('Y-m-d',$time);
			$weekList[$i]->week->week_describe=$weekList[$i]->week->week_describe.'['.$weekList[$i]->week->start_date.'至'.$endDate.']';
			$elem=$weekList[$i]->week->attributes;
			array_push($json,$elem);
		}
		
		echo CJSON::encode($json);
	}
	
	public function actionGetPeriodList()
	{
		$json=Period::model()->findAll();
		echo CJSON::encode($json);
	}
	
	public function actionShowHolidayPage()
	{
		$this->render('showHolidayPage');
	}
}
?>